package net.sunofbeach.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sunofbeach.blog.mapper.LooperMapper;
import net.sunofbeach.blog.pojo.Looper;
import net.sunofbeach.blog.pojo.query.LoopQuery;
import net.sunofbeach.blog.service.LooperService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Bing
 * @since 2021-11-24
 */
@Service
public class LooperServiceImpl extends ServiceImpl<LooperMapper, Looper> implements LooperService {

    /**
     * 条件分页查询轮播图
     *
     * @param pageParam
     * @param query
     * @return
     */
    @Override
    public IPage<Looper> selectPage(Page<Looper> pageParam, LoopQuery query) {
        LambdaQueryWrapper<Looper> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(query.getId())) {
            wrapper.eq(Looper::getId,query.getId());
        }
        if (!StringUtils.isEmpty(query.getTitle())) {
            wrapper.like(Looper::getTitle, query.getTitle());
        }
        if (!StringUtils.isEmpty(query.getTargetUrl())) {
            wrapper.like(Looper::getTargetUrl, query.getTargetUrl());
        }
        if (!StringUtils.isEmpty(query.getState())) {
            wrapper.eq(Looper::getState, query.getState());
        }
        if (!StringUtils.isEmpty(query.getBegin())) {
            wrapper.ge(Looper::getCreateTime, query.getBegin());
        }
        if (!StringUtils.isEmpty(query.getEnd())) {
            wrapper.le(Looper::getCreateTime, query.getEnd());
        }
        wrapper.orderByDesc(Looper::getCreateTime);

        return baseMapper.selectPage(pageParam, wrapper);
    }
}
